package com.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.ApiResult;
import com.exam.entity.ExamManage;
import com.exam.mapper.CollectionMapper;
import com.exam.service.CollectionService;
import com.exam.util.ApiResultHandler;
import com.exam.vo.CollectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//收藏试卷
@RestController
@Api(tags = "试卷模块")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;
    //收藏试卷
    @PostMapping("/collection")
    @ApiOperation(value = "收藏试卷", notes = "收藏试卷")
    public ApiResult collect(@RequestBody CollectVO collectVO){
        int count = 0;
        try {
            count = collectionService.collectPaper(collectVO);
        } catch (Exception e) {
            return ApiResultHandler.buildApiResult(400,"已经收藏该试卷",null);
        }
        if (count == 0){
            return ApiResultHandler.buildApiResult(401,"收藏失败",null);
        }
        return ApiResultHandler.success();
    }
    //取消收藏试卷
    @ApiOperation(value = "取消收藏试卷", notes = "取消收藏试卷")
    @PostMapping("/cancelCollection")
    public ApiResult cancelCollect(@RequestBody CollectVO collectVO){
        int count = collectionService.cancelCollect(collectVO);
        if (count == 0){
            return ApiResultHandler.buildApiResult(400,"取消收藏失败",null);
        }
        return ApiResultHandler.success();
    }
    @ApiOperation(value = "分页查询收藏试卷", notes = "查询收藏试卷")
    @GetMapping("/collectionList/{page}/{size}")
    public ApiResult findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @RequestParam("studentId") Integer studentId){
        System.out.println("分页查询收藏列表");
        ApiResult apiResult;
        Page<CollectVO> collection = new Page<>(page,size);
        IPage<CollectVO> all = collectionService.findAll(collection, studentId);
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", all);
        return apiResult;
    }
}
